﻿/*******************************************************
* Autor: Oleg Rukin aka Keyki
********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Keyki.InitData;
using Keyki.WpfNefclas.Helpers;

namespace Keyki.WpfNefclas
{
	/// <summary>
	/// Interaction logic for InitParams.xaml
	/// </summary>
	public partial class InitParams
	{
		private readonly SInitData _initData;
		private readonly List<ClassNameHelper> _classes;
		private readonly List<LinguisticVariableNameHelper> _lvNames;

		public InitParams(bool serial)
		{
			InitializeComponent();

            _classes = new List<ClassNameHelper>();
            _lvNames = new List<LinguisticVariableNameHelper>();
            _initData = Core.InitData;
            if (serial)
            {
                FeatureText.Text = Core.InitData.FeatureCount.ToString();
                SampleText.Text = Core.InitData.ElementCount.ToString();
                foreach (var name in _initData.ClassNames)
                {
                    _classes.Add(new ClassNameHelper { ClassName = name });    
                }
                foreach (var name in _initData.LinguisticVariablesNames)
                {
                    _lvNames.Add(new LinguisticVariableNameHelper{LinguisticVariablesName = name});
                }
            }
            else
            {
                _classes.Add(new ClassNameHelper());
                _lvNames.Add(new LinguisticVariableNameHelper());
            }
		}//InitParams

		private void WindowLoaded(object sender, RoutedEventArgs e)
		{
			ClassGrid.ItemsSource = _classes;
			LvGrid.ItemsSource = _lvNames;
			SampleText.PreviewTextInput		+= InputHelper.DecimalTextInput;
			FeatureText.PreviewTextInput	+= InputHelper.DecimalTextInput;
		}

		private void InitParamClosing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			_classes.RemoveAll(x => string.IsNullOrEmpty(x.ClassName));
			_lvNames.RemoveAll(x => string.IsNullOrEmpty(x.LinguisticVariablesName));
			Core.InitData.FeatureCount = Int32.Parse(FeatureText.Text);
			Core.InitData.ElementCount = Int32.Parse(SampleText.Text);
			Core.InitData.LinguisticVariables = _lvNames.Count;
			var resLv = from name in _lvNames select name.LinguisticVariablesName;
			Core.InitData.LinguisticVariablesNames = resLv.ToArray();
			var resClass = from nameHelper in _classes select nameHelper.ClassName;
			Core.InitData.ClassCount = _classes.Count;
			Core.InitData.ClassNames = resClass.ToArray();
		}
	}
}
